'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var atkData = require('../../../libs/war/atkData');
var bossData = require('../../../libs/war/bossData');
var constantUtil = require('../../../util/constantUtil');
var bossRecordModel = require('../../../models/bossRecordModel');
var PlayerManager = require('../../../libs/playerManager');
var bossService = require('../../../services/bossService');
var taskService = require('../../../services/taskService.js');
var soldierService = require('../../../services/soldierService.js');
var globalBossModel = require('../../../models/globalBossModel.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 进入boss战
 * 要给客户端下发的数据:
 * 1.敌方的建筑
 * 2.敌方的英雄,小兵
 * 3.我方的英雄,小兵
 */
Handler.prototype.enter = function (msg, session, next) {
    var role_id = session.uid;

    var boss_record_model;
    var atk_data;
    var def_data;

    if (!bossService.isCanEnter()) {
        return next(null, {
            code: Code.BOSS_CAN_NOT_ENTER
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['boss', 'soldier', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            function (cb) {
                var boss_model = player_manager.boss;
                var soldier_model = player_manager.soldier;

                // 更新战力
                boss_model.fight_power = soldierService.calcSoldierPower(soldier_model, boss_model.select_soldier_list);

                if (!boss_model.subBattleNum()) {
                    return next(null, {
                        code: Code.BOSS_BATTLE_NUM_EMPTY
                    });
                }

                cb();
            },
            function (cb) {
                globalBossModel.getGlobalBossInfo(cb);
            },
            function (model, cb) {
                def_data = _getDef(model.boss_id);

                // 我方数据
                _getAtk(role_id, cb);
            },
            function (data, cb) {
                atk_data = data;

                // 存储战斗记录
                bossRecordModel.addRecord(
                    role_id,
                    atk_data,
                    def_data,
                    cb);
            },
            function (model, cb) {
                boss_record_model = model;

                // 世界BOSS参与次数统计
                taskService.addJoinChatBossNum(player_manager);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    record_id: boss_record_model.getRecordId(),
                    atk: atk_data,
                    def: def_data
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, cb) {
    atkData.getDataForBossWar(role_id, cb);
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (boss_id) {
    return bossData.getData(boss_id);
};
